package com.deyuanyun.pic.ctrl.service.impl;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.ctrl.controller.vo.defect.DefectStatisticsVO;
import com.deyuanyun.pic.ctrl.controller.vo.defect.RepairLeakagePointVO;
import com.deyuanyun.pic.ctrl.mapper.CtrlRepairTaskMapper;
import com.deyuanyun.pic.ctrl.mapper.dao.OclDefectRepairMgrDao;
import com.deyuanyun.pic.pbf.controller.dto.defect.node.*;
import com.deyuanyun.pic.pbf.enumeration.DefectRepairNode;
import com.deyuanyun.pic.pbf.mapper.dao.DefectInfoDao;
import com.deyuanyun.pic.pbf.mapper.dao.DefectRepairNodeDao;
import com.deyuanyun.pic.ctrl.service.OclDefectRepairMgrService;
import com.deyuanyun.pic.wf.domain.TaskInstance;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缺陷修复流程管理服务
 *
 * @author hlw
 * @date
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class OclDefectRepairMgrServiceImpl implements OclDefectRepairMgrService {

    @Resource
    private DefectRepairNodeDao defectRepairNodeDao;
    @Resource
    private DefectInfoDao defectInfoDao;

    @Resource
    private OclDefectRepairMgrDao oclDefectRepairMgrDao;
    @Resource
    private CtrlRepairTaskMapper ctrlRepairTaskMapper;

    private Logger logger = LogManager.getLogger(this.getClass());

    @Override
    public DefectStatisticsVO getDefectStatistics(String repairTaskId){
        DefectStatisticsVO defectStatisticsVO = new DefectStatisticsVO();
        defectStatisticsVO.fromDomain(ctrlRepairTaskMapper.selectByPrimaryKey(repairTaskId));
        defectStatisticsVO.fromDefectStatistics(oclDefectRepairMgrDao.getDefectStatistics(repairTaskId));
        return defectStatisticsVO;
    }

    @Override
    public List<Map<DefectRepairNode, Object>> queryRepairNode(String defectId) {

        // 1,根据defectId 获得该缺陷对应的维修节点TaskInstance
        // 2,根据TaskInstance对象找到数据存储的表单，并取出对应的数据

        List<Map<DefectRepairNode, Object>> list = new ArrayList();

        List<TaskInstance> taskInstanceList = defectInfoDao.queryTaskInstanceList(defectId);
        for (TaskInstance taskInstance : taskInstanceList){
            Map<DefectRepairNode, Object> defectRepairNodeMap = this.getDefectRepairNode(taskInstance);
            if (defectRepairNodeMap != null) {
                list.add(defectRepairNodeMap);
            }
        }

        return list;
    }

    /**
     * 得到缺陷维修节点
     *
     * @param taskInstance
     * @return
     */
    private Map<DefectRepairNode, Object> getDefectRepairNode(TaskInstance taskInstance) {

        if (ObjectUtil.isEmptyAll(taskInstance)){
            return null;
        }

        String taskInstanceId = taskInstance.getId();
        String nodeType = taskInstance.getNodeType();
        Short isCompletion = taskInstance.getIsCompletion();

        // 1,判断是否是：管沟开挖 ok
        if (DefectRepairNode.START_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【管沟开挖】节点数据");
            TrenchExcavationDto trenchExcavationDto = new TrenchExcavationDto(isCompletion);
            trenchExcavationDto.fromDomain(defectRepairNodeDao.findTrenchExcavation(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.START_NODE, trenchExcavationDto);
        }
        // 2,判断是否是：腐蚀环境调查 ok
        if (DefectRepairNode.CORROSION_ENVIRONMENT_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【腐蚀环境调查】节点数据");
            CorrosionEnvironmentDto corrosionEnvironmentDto = new CorrosionEnvironmentDto(isCompletion);
            corrosionEnvironmentDto.fromDomain(defectRepairNodeDao.findCorrosionEnvironment(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.CORROSION_ENVIRONMENT_NODE, corrosionEnvironmentDto);
        }
        // 3,判断是否是：漏损点测量
        if (DefectRepairNode.CTRL_LEAKAGE_POINT_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【漏损点测量】节点数据");
            RepairLeakagePointVO repairLeakagePointVO = new RepairLeakagePointVO(isCompletion);
            repairLeakagePointVO.fromDomain(oclDefectRepairMgrDao.findRepairLeakagePoint(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.CTRL_LEAKAGE_POINT_NODE, repairLeakagePointVO);
        }
        // 4,判断是否是：防腐层调查 ok
        if (DefectRepairNode.COAT_SURVEY_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【防腐层调查】节点数据");
            CoatSurveyDto coatSurveyDto = new CoatSurveyDto(isCompletion);
            coatSurveyDto.fromDomain(defectRepairNodeDao.findCoatSurvey(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.COAT_SURVEY_NODE, coatSurveyDto);
        }
        // 5,判断是否是：表面处理 ok
        if (DefectRepairNode.SURFACE_TREATMENT_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【表面处理】节点数据");
            SurfaceTreatmentDto surfaceTreatmentDto = new SurfaceTreatmentDto(isCompletion);
            surfaceTreatmentDto.fromDomain(defectRepairNodeDao.findSurfaceTreatment(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.SURFACE_TREATMENT_NODE, surfaceTreatmentDto);
        }
        // 6,判断是否是：阴保调查 ok
        if (DefectRepairNode.CP_SURVEY_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【阴保调查】节点数据");
            CPSurveyDto cpSurveyDto = new CPSurveyDto(isCompletion);
            cpSurveyDto.fromDomain(defectRepairNodeDao.findCPSurvey(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.CP_SURVEY_NODE, cpSurveyDto);
        }
        // 7,判断是否是：防腐层修复 ok
        if (DefectRepairNode.COAT_REPAIR_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【防腐层修复】节点数据");
            CoatRepairDto coatRepairDto = new CoatRepairDto(isCompletion);
            coatRepairDto.fromDomain(defectRepairNodeDao.findCoatRepair(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.COAT_REPAIR_NODE, coatRepairDto);
        }
        // 8,判断是否是：管沟回填 ok
        if (DefectRepairNode.END_NODE.toString().equals(nodeType)) {
            logger.debug("----------->加载缺陷管理-【管沟回填】节点数据");
            TrenchBackPillDto trenchBackPillDto = new TrenchBackPillDto(isCompletion);
            trenchBackPillDto.fromDomain(defectRepairNodeDao.findTrenchBackPill(taskInstanceId));
            return this.addDefectNode(DefectRepairNode.END_NODE, trenchBackPillDto);
        }
        return null;
    }

    private Map addDefectNode(DefectRepairNode defectRepairNode, Object o) {
        Map<DefectRepairNode, Object> map = new HashMap<DefectRepairNode, Object>();
        map.put(defectRepairNode, o);
        return map;
    }

}
